<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 9.3.&nbsp; Using PEAR</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-9-SECT-2.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-9-SECT-4.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="learnphpmysql-CHP-9-SECT-3"></a>
<h3 id="title-IDADWGYH" class="docSection1Title">9.3. Using PEAR</h3>
<a name="IDX-CHP-9-0436"></a> 
<a name="IDX-CHP-9-0437"></a> 
<a name="IDX-CHP-9-0438"></a> 
<a name="IDX-CHP-9-0439"></a> 

<p class="docText">PEAR is a framework and distribution system for reusable PHP components. Actually, PEAR is a collection of add-on functionality for PHP development. There are many modules available to handle everything from session management to shopping cart functionality. Modules that are currently available are listed in <a class="docLink" href="#learnphpmysql-CHP-9-TABLE-1">Table 9-1</a>.</p>
<a name="learnphpmysql-CHP-9-TABLE-1"></a><p><table cellspacing="0" FRAME="hsides" RULES="all" cellpadding="4" width="100%"><caption><h5 class="docTableTitle">Table 9-1. PEAR modules</h5></caption><colgroup span="3"><col><col><col></colgroup><thead><tr><th class="thead" scope="col" align="left"><p class="docText">Authentication</p></th><th class="thead" scope="col" align="left"><p class="docText">HTML</p></th><th class="thead" scope="col" align="left"><p class="docText">Processing</p></th></tr></thead><tr><TD class="docTableCell" align="left"><p class="docText">Benchmarking</p></td><TD class="docTableCell" align="left"><p class="docText">HTTP</P></td><TD class="docTableCell" align="left"><p class="docText">Science</p></td></tr><TR><td class="docTableCell" align="left"><p class="docText">Caching</p></TD><td class="docTableCell" align="left"><p class="docText">Images</p></TD><td class="docTableCell" align="left"><p class="docText">Semantic Web</p></td></tr><TR><TD class="docTableCell" align="left"><p class="docText">Configuration</p></td><td class="docTableCell" align="left"><p class="docText">Internationalization</p></TD><td class="docTableCell" align="left"><p class="docText">Streams</P></td></TR><TR><TD class="docTableCell" align="left"><p class="docText">Console</p></td><td class="docTableCell" align="left"><p class="docText">Logging</p></td><td class="docTableCell" align="left"><p class="docText">Structures</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText">Database</p></td><td class="docTableCell" align="left"><p class="docText">Mail</P></td><td class="docTableCell" align="left"><p class="docText">System</P></TD></tr><TR><td class="docTableCell" align="left"><p class="docText">Date/Time</p></td><TD class="docTableCell" align="left"><p class="docText">Math</p></td><TD class="docTableCell" align="left"><p class="docText">Test</p></td></TR><tr><td class="docTableCell" align="left"><p class="docText">Encryption</p></td><TD class="docTableCell" align="left"><p class="docText">Networking</P></td><td class="docTableCell" align="left"><p class="docText">Tools &amp; Utilities</p></td></TR><tr><TD class="docTableCell" align="left"><p class="docText">Event</p></TD><TD class="docTableCell" align="left"><p class="docText">Numbers</P></td><td class="docTableCell" align="left"><p class="docText">Validate</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText">File Formats</p></td><td class="docTableCell" align="left"><p class="docText">Payment</p></td><td class="docTableCell" align="left"><p class="docText">Web Services</p></TD></tr><tr><TD class="docTableCell" align="left"><p class="docText">File System</P></td><TD class="docTableCell" align="left"><p class="docText">PEAR</p></td><td class="docTableCell" align="left"><p class="docText">XML</P></td></tr><TR><td class="docTableCell" align="left"><p class="docText">GTK Components</p></TD><td class="docTableCell" align="left"><p class="docText">PHP</p></td><td class="docTableCell">&nbsp;</TD></TR></table></p><br>
<p class="docText">Our list is not complete. Visit <a class="docLink" target="_blank" href="http://pear.php.net">http://pear.php.net</a> to find out all of the modules that are available for download.</p>
<a name="learnphpmysql-CHP-9-SECT-3.1"></a>
<h4 id="title-IDA53GYH" class="docSection2Title">9.3.1. Installing</H4>
<p class="docText">PEAR uses a Package Manager to manage which PEAR features you install. Whether you need to install the Package Manager depends on which version of PHP you installed. If you're running PHP 4.3.0 or newer, it's already installed. If you're running PHP 5.0, PEAR has been split out into a separate package. The DB package that you're interested in is also installed by default with the Package Manager. So if you have the Package Manger, you're all set.</p>
<a name="learnphpmysql-CHP-9-SECT-3.1.1"></a>
<H5 id="title-IDAH4GYH" class="docSection3Title">9.3.1.1. Unix</h5>
<a name="IDX-CHP-9-0440"></a> 
<a name="IDX-CHP-9-0441"></a> 

<p class="docText">You can install the Package Manager on a Unix system by executing the following from the shell (command-line) prompt:</P>
<pre>
lynx -source http://go-pear.org/ | php
</pre><BR>

<p class="docText">This takes the output of the <tt><B>go-pear.org</b></tt> site (which is actually the source PHP code) to install PEAR and passes it along to the <tt>php</tt> command for execution.</p>

<a name="learnphpmysql-CHP-9-SECT-3.1.2"></a>
<h5 id="title-IDAF5GYH" class="docSection3Title">9.3.1.2. Windows</h5>
<a name="IDX-CHP-9-0442"></a> 
<a name="IDX-CHP-9-0443"></a> 
<a name="IDX-CHP-9-0444"></a> 

<p class="docText">The PHP 5 installation includes the PEAR installation script as <span class="docEmphasis">C:\php\go-pear.bat</span>. In case you didn't install all the files in <a class="docLink" href="learnphpmysql-CHP-2.html#learnphpmysql-CHP-2">Chapter 2</a>, go ahead and extract all the PHP files to <span class="docEmphasis">C:/php</span> from the command prompt, and execute the <span class="docEmphasis">.bat</span> file. <a class="docLink" href="#learnphpmysql-CHP-9-FIG-5">Figure 9-5</a> shows the initial screen after executing the PEAR installer.</p>
<a name="learnphpmysql-CHP-9-FIG-5"></a><p><center>
<h5 class="docFigureTitle">Figure 9-5. The go-pear.bat install script</h5>
<img border="0" alt="" width="549" height="160" SRC="images/learnphpmysql_0905.jpg">
</center></p><br>
<p class="docText">You'll be asked a set of questions about paths. You can accept the defaults for all of them.</p>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><td><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><tr><TD width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></td><td valign="top">
<p class="docText">The <span class="docEmphasis">php.exe</span> file must be in your path. Verify by typing <tt>php.exe</tt> from a command prompt. If it is not found, you'll need to add it to your <tt>PATH</tt> variable. To access your system path, navigate to Start <img src=images/U2192.jpg border=0> Control Panel <img src=images/U2192.jpg border=0> System <img src=images/U2192.jpg border=0> Environment and add an entry to the end of the path with <tt>C:\php</tt>.</P>
</TD></tr></table></TD></tr></table></p><br>
<p class="docText">The PEAR installer creates a file called <span class="docEmphasis">C:\php\PEAR_ENV.reg</span>. You need to double-click to set up the PEAR paths in the registry. This file is contingent on which PEAR version you installed. When the dialog box appears to verify your information, you will add this to the registry and click OK.</P>
<p class="docText">You may have to edit the <span class="docEmphasis">php.ini</span> file after running this <span class="docEmphasis">.bat</span> file to add the PEAR directory to the include path. Line 447 of <span class="docEmphasis">php.ini</span> now looks like this:</p>
<pre>
include_path = ".;c:\php\includes;c:\php\PEAR"
</pre><br>

<p class="docText">Apache must be restarted before the DB package can be used.</P>

<a name="learnphpmysql-CHP-9-SECT-3.1.3"></a>
<h5 id="title-IDADCHYH" class="docSection3Title">9.3.1.3. Hosted ISP</h5>
<p class="docText">Most ISPs have PEAR DB installed. Ask your ISP to install it if they haven't already. You can tell if PEAR DB has been installed by trying the PHP code in <a class="docLink" href="#learnphpmysql-CHP-9-EX-7">Example 9-7</a> to see whether the <tt>require_once ('DB.php');</tt> line causes an error when the script is executed.</P>


<a name="learnphpmysql-CHP-9-SECT-3.2"></a>
<h4 id="title-IDAVCHYH" class="docSection2Title">9.3.2. Adding Additional Packages</h4>
<a name="IDX-CHP-9-0445"></a> 

<p class="docText">Once that's complete, you can access the PEAR Package Manger by entering <tt>pear</tt> at the command prompt. Adding new modules is as easy as executing <tt>pear</tt> <tt><i>packagename</i></tt>. You won't need to do anything, since the DB package was installed along with the install by default.</P>
<p class="docText">However, if you're running Windows XP Home, you'll need to take these steps to install the PEAR DB:</P>
<pre>
C:\&gt;cd c:\php
C:\&gt;pear install DB
C:\&gt;pear list
</pre><br>

<p class="docText">To find out what versions of PEAR packages<a name="IDX-CHP-9-0446"></a> 
 are installed, execute <tt>pear list</tt>. That returns a listing such as the one shown in <a class="docLink" href="#learnphpmysql-CHP-9-FIG-6">Figure 9-6</a>.</p>
<a name="learnphpmysql-CHP-9-FIG-6"></a><p><center>
<h5 class="docFigureTitle">Figure 9-6. A listing of installed PEAR packages and versions</H5>
<img border="0" alt="" width="549" height="200" SRC="images/learnphpmysql_0906.jpg">
</center></p><BR>
<p class="docText">Once you've got PEAR installed, you're ready to try it out.</p>

<a name="learnphpmysql-CHP-9-SECT-3.3"></a>
<H4 id="title-IDALEHYH" class="docSection2Title">9.3.3. Rewriting the Books Example with PEAR</H4>
<p class="docText">When using the PEAR DB package, you follow the same steps. However, the function syntax is slightly different. We'll go line by line and explain the differences as they appear in <a class="docLink" href="#learnphpmysql-CHP-9-EX-7">Example 9-7</a>.</P>
<a name="learnphpmysql-CHP-9-EX-7"></a><h5 id="title-IDAZEHYH" class="docExampleTitle">Example 9-7. Displaying the books table with PEAR DB</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
 1 &lt;?php
 2
 3 include('db_login.php');
 4 require_once('DB.php');
 5
 6 $connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
 7
 8 if (DB::isError($connection)){
 9 die("Could not connect to the database: &lt;br /&gt;".DB::errorMessage($connection));
 10 }
 11
 12 $query = "SELECT * FROM `books` NATURAL JOIN `authors`";
 13 $result = $connection-&gt;query($query);
 14
 15 if (DB::isError($result)){
 16 die("Could not query the database:&lt;br /&gt;".$query." ".DB::errorMessage($result));
 17 }
 18
 19 echo('&lt;table border="1"&gt;');
 20 echo '&lt;tr&gt;&lt;th&gt;Title&lt;/th&gt;&lt;th&gt;Author&lt;/th&gt;&lt;th&gt;Pages&lt;/th&gt;&lt;/tr&gt;';
 21
 22 while ($result_row = $result-&gt;fetchRow()) {
 23 echo "&lt;tr&gt;&lt;td&gt;";
 24 echo $result_row[1] . '&lt;/td&gt;&lt;td&gt;';
 25 echo $result_row[4] . '&lt;/td&gt;&lt;td&gt;';
 26 echo $result_row[2] . '&lt;/td&gt;&lt;/tr&gt;';
 27 }
 28
 29 echo("&lt;/table&gt;");
 30 $connection-&gt;disconnect();
 31
 32 ?&gt;
</pre><br>

<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-9-EX-7">Example 9-7</a> displays the screen shown in <a class="docLink" href="#learnphpmysql-CHP-9-FIG-7">Figure 9-7</a>.</p>
<a name="learnphpmysql-CHP-9-FIG-7"></a><p><center>
<h5 class="docFigureTitle">Figure 9-7. Switching to the PEAR DB functions didn't change the output</h5>
<img border="0" alt="" width="525" height="232" SRC="images/learnphpmysql_0907.jpg">
</center></p><br>
<p class="docText">Notice that <a class="docLink" href="#learnphpmysql-CHP-9-FIG-7">Figure 9-7</a> is identical to the output in <a class="docLink" href="learnphpmysql-CHP-9-SECT-2.html#learnphpmysql-CHP-9-FIG-4">Figure 9-4</a>.</p>
<p class="docText">Line 3 includes your database login information and remains unchanged:</p>
<pre>
include('db_login.php');
</pre><BR>

</td></tr></table></P>
<p class="docText">Line 4 has a new <tt>require</tt> statement:</P>
<pre>
require_once( "DB.php" );
</pre><br>

<p class="docText">This requires the file <span class="docEmphasis">DB.php</span>, which provides the PEAR DB functions. The <tt>require_once</tt> function errors out if the <span class="docEmphasis">DB.php</span> file is not found. It also will not include the file if it has been incorporated already. And, this would cause an error.</P>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><td><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><TR><td width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></td><TD valign="top">
<p class="docText">The file <span class="docEmphasis">DB.php</span> is found in the <span class="docEmphasis">/pear</span> subdirectory of the PHP distribution. The PEAR install should have added that directory to the <tt>include_path</tt> in the <span class="docEmphasis">php.ini</span> file. If this file is not found, verify that PEAR DB is installed and that the paths are set up correctly.</p>
</td></TR></table></td></tr></table></p><br>
<a name="learnphpmysql-CHP-9-SECT-3.3.1"></a>
<H5 id="title-IDAMHHYH" class="docSection3Title">9.3.3.1. Creating a connect instance</H5>
<a name="IDX-CHP-9-0447"></a> 

<p class="docText">The <span class="docEmphasis">DB.php</span> file defines a class of type DB. Refer to <a class="docLink" href="learnphpmysql-CHP-5.html#learnphpmysql-CHP-5">Chapter 5</a> for more information on working with classes and objects. We'll principally be calling the methods in the class. The DB class has a <tt>connect</tt> method, which we'll use instead of our old connect function <tt>mysql_connect</tt>. The double colons (<tt>::</tt>) indicate that we're calling that function from the class in line 4:</p>
<pre>
connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
</pre><br>

<p class="docText">When you call the <tt>connect</tt> function, it creates a new database connection that is stored in the variable <tt>$connection</tt>. The connect function attempts to connect to the database based on the connect string you passed to it.</p>

<a name="learnphpmysql-CHP-9-SECT-3.3.2"></a>
<h5 id="title-IDAVIHYH" class="docSection3Title">9.3.3.2. Connect string</H5>
<p class="docText">The connect string uses this new format to represent the login information that you already supplied in separate fields:</p>
<pre>
<tt><I>dbtype://username:password@host/database</i></tt>
</pre><BR>

<p class="docText">This format may look familiar to you, as it's very similar to the connect string for a Windows share. The first part of the string is what really sets the PEAR functions apart from the plain PHP. The <tt>phptype</tt> field specifies the type of database to connect. Supported databases include <tt>ibase, msql, mssql, mysql, oci8, odbc, pgsql</tt>, and <tt>sybase</tt>. All that's required for your PHP page to work with a different type of database is changing the <tt>phptype</tt>!</P>
<p class="docText">The <tt><I>username</i></tt>, <tt><i>password</i></tt>, <tt><i>host</i></tt>, and <tt><i>database</i></tt> should be familiar from the basic PHP connect. Only the type of connection is required. However, you'll usually want to specify all fields.</p>
<p class="docText">After the values from <span class="docEmphasis">dblogin.php</span> are included, the <tt>connect</tt> string looks like the following:</p>
<pre>
"mysql://test:test@localhost/test"
</pre><br>

<p class="docText">If the <tt>connect</tt> method on line 6 was successful, a <tt>$DB</tt> object is created. It contains the methods to access the database as well as all of the information about the state of that database connection.</p>

<a name="learnphpmysql-CHP-9-SECT-3.3.3"></a>
<h5 id="title-IDAJKHYH" class="docSection3Title">9.3.3.3. Querying</h5>
<a name="IDX-CHP-9-0448"></a> 
<a name="IDX-CHP-9-0449"></a> 
<a name="IDX-CHP-9-0450"></a> 
<a name="IDX-CHP-9-0451"></a> 
<a name="IDX-CHP-9-0452"></a> 

<p class="docText">One of the methods it contains is called <tt>query</tt>. The <tt>query</tt> method works just like PHP's <tt>query</tt> function in that it takes a SQL statement. The difference is the hyphen and greater-than syntax (<tt>-&gt;</tt>) is used to call it from the object. It also returns the results as another object instead of a result set.</p>
<pre>
$query = "SELECT * FROM `books`"
$result = $connection-&gt;query($query);
</pre><BR>

<p class="docText">Based on the SQL query, this code calls the query function from the connection object and returns a result object named <tt>$result</tt>.</p>

<a name="learnphpmysql-CHP-9-SECT-3.3.4"></a>
<h5 id="title-IDA0LHYH" class="docSection3Title">9.3.3.4. Fetching</H5>
<p class="docText">Line 22 uses the result object to call the <tt>fetchRow</tt> method. It returns the rows one at a time, similar to <tt>mysql_fetch_row</tt>.</P>
<pre>
while ($result_row = $result-&gt;fetchRow()) {
    echo 'Title: '.$result_row[1] . '&lt;br /&gt;';
    echo 'Author: '.$result_row[4] . '&lt;br /&gt; ';
    echo 'Pages: '.$result_row[2] . '&lt;br /&gt;&lt;br /&gt;';
}
</pre><br>

<p class="docText">You use another <tt>while</tt> loop to go through each row from <tt>fetchRow</tt> until it returns <tt>FALSE</tt>. The code in the loop hasn't changed from the non-PEAR<a name="IDX-CHP-9-0453"></a> 
 example.</P>

<a name="learnphpmysql-CHP-9-SECT-3.3.5"></a>
<h5 id="title-IDAYMHYH" class="docSection3Title">9.3.3.5. Closing</h5>
<p class="docText">In line 30, you're finished with the database connection, so you close it using the object method <tt>disconnect</tt>:</p>
<pre>
$connection-&gt;disconnect();
</pre><BR>


<a name="learnphpmysql-CHP-9-SECT-3.3.6"></a>
<h5 id="title-IDAGNHYH" class="docSection3Title">9.3.3.6. PEAR error reporting</h5>
<a name="IDX-CHP-9-0454"></a> 

<p class="docText">The function <tt>DB::isError</tt> will check to see whether the result that's been returned to you is an error or not. If it is an error, you can use <tt>DB::errorMessage</tt> to return a text description of the error that was generated. You need to pass <tt>DB::errorMessage</tt> the return value from your function as an argument.</P>
<p class="docText">Here you rewrite the PEAR code to use error checking:</p>
<pre>
&lt;?php
if ( DB::isError( $demoResult = $db-&gt;query( $sql)))
{
    echo DB::errorMessage($demoResult);
} else {
    while ($demoRow = $demoResult-&gt;fetchRow()) {
           echo $demoRow[2] . '&lt;br /&gt;';
    }
}
?&gt;
</pre><br>

<p class="docText">Now that you have a good handle on connecting to the database and the various functions of PEAR, we're going to talk about forms. Forms provide a way to send substantial data from the user to the server where it can be processed.</P>



<a href="11011536.html"><img src="images/pixel.jpg" alt="" width="1" height="1" border="0"></a></TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-9-SECT-2.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-9-SECT-4.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</html>
